#!python3
# xlutils_demo.py
# 使用第三方库xlutils实现追写Excel:先复制一份Sheet然后在此基础上追加并保存到一份新的Excel

import os
from xlutils.copy import copy
import xlrd


def write_append(file):

    wb = xlrd.open_workbook(file)
    sheet = wb.sheet_by_index(0)
    rows = sheet.nrows

    # 拷贝一个新的xls文件
    newWb = copy(wb)
    sheet = newWb.get_sheet(0)

    values = ["Ann", "woman", 22, "UK"]
    # 追加内容
    for i in range(0, len(values)):
        sheet.write(rows, i, values[i])  # row, column, value

    # 取文件名和文件类型
    filename, suffix = os.path.splitext(file)
    path = os.path.dirname(filename)
    newWb.save(os.path.join(path, "xlutils_demo"+suffix));


if __name__ == "__main__":
    write_append("../files/openpyxl_read.xlsx")
